package com.djz.stuinfomge.service.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.djz.stuinfomge.dao.BaseDao;
import com.djz.stuinfomge.service.BaseService;



public class BaseServiceImpl<T> implements BaseService<T> {
	public BaseServiceImpl() {
		Type t = getClass().getGenericSuperclass();
		// instanceof判断，是为没有参数类型的子类服务的
		if (t instanceof ParameterizedType) {
			ParameterizedType pt = (ParameterizedType) t;
			this.clazz = (Class<T>) pt.getActualTypeArguments()[0];
		}
	}

	public Serializable add(T t) {

		Serializable id = ht.save(t);
		return id;
	}

	public T get(int id) {
		return ht.get(clazz, id);

	}

	public void remove(int id) {
		T t = ht.get(clazz, id);
		
		ht.delete(t);
	}

	public void update(T t) {
		ht.update(t);

	}

	protected BaseDao<T> dao;

	protected Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);
	// 使用hibernateTemplate，对于某些实体类，就可以不用创建dao实例。
	@Autowired
	private HibernateTemplate ht;

	private Class<T> clazz;
}
